/* 
 * Copyright (C) 2008-2010 Josef Hardi <josef.hardi@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.bth.student.android.photohand.parser;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.util.Log;
import edu.bth.student.android.photohand.provider.PhotoHandSchema.Photos;

public class PicasaWebPhotoHandler extends DefaultHandler {

  private static final String TAG = "PHOTO HANDLER";

  private static final String ATOM_NAMESPACE = "http://www.w3.org/2005/Atom";
  private static final String MEDIA_NAMESPACE = "http://search.yahoo.com/mrss/";

  private boolean entryTag = false;
  private boolean photoTitleTag = false;
  private boolean photoUrlTag = false;
  private boolean photoPublishedTag = false;

  private ContentResolver content;
  private ContentValues values;

  private Uri albumUri;

  private StringBuilder builder;
  private int counter = 0;

  public PicasaWebPhotoHandler(ContentResolver content, Uri albumUri) {
    this.content = content;
    this.albumUri = albumUri;
    this.values = new ContentValues();
  }

  @Override
  public void startDocument() throws SAXException {
    // Do nothing
  }

  @Override
  public void endDocument() throws SAXException {
    // Do nothing
  }

  @Override
  public void startElement(String uri, String localName, String qName,
      Attributes attributes) throws SAXException {
    if (uri.equals(ATOM_NAMESPACE)) {
      if (localName.equals("entry")) {
        entryTag = true;
        values.clear();
      }

      if (entryTag) {
        if (localName.equals("title")) {
          photoTitleTag = true;
          builder = new StringBuilder();
        } else if (localName.equals("published")) {
          photoPublishedTag = true;
          builder = new StringBuilder();
        }
      }
    }

    if (uri.equals(MEDIA_NAMESPACE)) {
      if (entryTag) {
        if (localName.equals("thumbnail")) {
          counter++;
          if (counter == 3) {
            Log.w(TAG, attributes.getValue("url"));
            values.put(Photos.URL, attributes.getValue("url"));
            counter = 0; // reset the counter
          }
        }
      }
    }
  }

  @Override
  public void endElement(String uri, String localName, String qName)
      throws SAXException {
    if (uri.equals(ATOM_NAMESPACE)) {
      if (localName.equals("entry")) {
        entryTag = false;
        Uri photoUri = Uri.withAppendedPath(albumUri, "photos");
        content.insert(photoUri, values);
      }

      if (entryTag) {
        if (localName.equals("title")) {
          photoTitleTag = false;
          values.put(Photos.TITLE, getText());
        } else if (localName.equals("published")) {
          photoPublishedTag = false;
          values.put(Photos.PUBLISHED_ON, getText());
        }
      }
    }
  }

  @Override
  public void characters(char text[], int start, int length) {
    if (builder != null) {
      builder.append(text, start, length);
    }
  }

  String getText() {
    try {
      return builder.toString();
    } finally {
      builder = null;
    }
  }
}
